package org.jugile.web2;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.jugile.util.Blob;
import org.jugile.util.Jugile;
import org.jugile.util.Props;

public abstract class Component extends Jugile implements Serializable {

	// --- convinience helpers ---
	public String param(String key) { return cycle.getParam(key); }
	public List<String> paramNames() { return cycle.getParamNames(); }
	public List<String> paramValues(String key) { return cycle.getParamValues(key); }
	public Blob file(String key) { return cycle.getFile(key); }
	public List<String> fileNames() { return cycle.getFileNames(); }
	public String getBody() { return cycle.getBody(); }
	
	//public String getCtxpath() { return cycle.req.getCtxPath(); }
	public Object ctrl(String name) { return cycle.getController(name); }
	public Cycle cycle() { return cycle; }

	public Object getVar(String name) { return cycle.getVar(name); }
	public void setVar(String name, Object o) { cycle.setVar(name, o); }
	public Map<String,Object> getAttrs() { return cycle.getAttrs(); }
	
	public String getStatic() {	return Props.get("jugile.static"); }
	
	// for migration
	public void put(String key) { put(key, arg()); }
	public void put(String key, String value) { cycle.put(key,value);}
	public String get(String key) { return cycle.get(key); }
	public String arg() { return param("arg"); }
	public String getAction(String listener) { return "a_"+listener; }
	
	// --- default base methods ---
	public Response get() { return null; }
	public Response post() { return null; }
	public Response put() { return null; }
	
	// --- init ---
	private Cycle cycle = null;
	public void init(Cycle cycle) {
		this.cycle = cycle;
		init();
	}

	public String getName() { 
		return getClassName(getClass());
	}
	
	private String getTmplSuffix() {
		return Props.get("jugile.web.tmpl.suffix",".xml");
	}

	private String tmpl = "";
	public void setTmpl(String name) { tmpl = name; }
	public String getTmpl() {
		if (tmpl == null) return null;
		if (!empty(tmpl)) return tmpl;
		
		return getTemplatePath() + "/" + getName() + getTmplSuffix();
	}
	
	private String mime = null;
	public void setMimetype(String v) { mime = v; }
	public String getMimetype() { return mime; }

	public String getStatus() { return null; } // override this if needed

	public abstract boolean needsAuthentication();
	public void init() {} // override this if needed
	public String getContent(VeloEngine ve) { return null; } // override if direct content

	public String include(String cname, Object model) {
		return cycle().evalTemplate(cname, model);
	}
	
	public String getBorder(String name) {
		if (empty(name)) name = "Border";
		return getTemplatePath() + "/"+name+getTmplSuffix();
	}
	
	public String getTemplatePath() {
		if (!empty(Props.get("jugile.web.tmpl"))) {
			return Props.get("jugile.web.tmpl");
		}
		return getObjectPath(this);
	}

	
	// ----- for testing -----
	public void param(String key, String value) { cycle.setParam(key, value); }
	
}
